<?php

namespace app\controllers\admin\uni\system;

use app\controllers\admin\base\AdminController;
use app\models\SetTabbar as Model;

class TabbarController extends AdminController
{
    public function actionList()
    {
        // $name = $this->post('name');
        // $id = $this->post('id');
        // $status = $this->post('status');
        // $export = $this->post('export');

        $page = intval($this->post('page', $this->page));
        $page_size = intval($this->post('pageSize', $this->pageSize));

        $sort = $this->post('sort');

        $query = Model::find()
            // ->where(['status' => 1])
            ->with(['icon', 'activeIcon']);

        // if ($status > Model::$statusMapping['ALL']) {
        //     $query->andWhere(['status' => $status]);
        // }

        // if ($sort) {
        //     $prop = substr($sort, 0, 1);
        //     $prop = $prop == '-' ? SORT_DESC : SORT_ASC;
        //     $order = substr($sort, 1);
        //     $query->orderBy([$order => $prop]);
        // } else {
        //     $query->orderBy(['sort' => SORT_ASC, 'id' => SORT_DESC]);
        // }

        $query->orderBy(['sort' => SORT_ASC, 'id' => SORT_ASC]);

        // if ($id) {
        //     $query->andWhere(['id' => $id]);
        // }
        // if ($name) {
        //     $query->andWhere(['like', 'name', $name]);
        // }
        //
        // // export
        // if ($export) {
        //     return $this->success($query->asArray()->all());
        // }

        $totalCount = $query->count();
        $totalPage = ceil($totalCount / $page_size);

        $list = $query->offset(($page - 1) * $page_size)
            ->limit($page_size)
            ->asArray()
            ->all();

        return $this->success([
            $this->totalCount   => $totalCount,
            $this->totalPage    => $totalPage,
            $this->list         => $list,
        ]);

    }

    public function actionEdit()
    {
        $data = $this->post();

        try {
            if (isset($data['id']) && $data['id'] > 0) {
                $model = Model::findOne(['id' => $data['id']]);
                if (!$model) {
                    return $this->fail('操作失败');
                }
            } else {
                $model = new Model();
            }
            $data = ['SetTabbar' => $data];
            if ($model->load($data) && $model->save()) {
                return $this->success($model, '操作成功');
            }
        } catch (\Exception $e) {
            return $this->fail($e->getMessage());
        }
    }

    public function actionView()
    {
        $id = $this->post('id');
        $model = Model::find()
            ->where(['id' => $id])
            ->with(['icon', 'activeIcon'])
            ->asArray()
            ->one();

        if ($model) {
            return $this->success($model);
        }

        return $this->fail('不存在');
    }

    // public function actionRemove()
    // {
    //     $id = $this->post('id');
    //
    //     try {
    //         $model = Model::findOne(['id' => $id]);
    //         $model->status = Model::$statusMapping['DELETE'];
    //         $model->save();
    //         return $this->success($model, '删除成功');
    //     } catch (\Exception $e) {
    //         return $this->fail($e->getMessage());
    //     }
    // }

    // public function actionStatusList()
    // {
    //     return $this->success(Model::statusList());
    // }

    public function actionChangeStatus()
    {
        $id = intval($this->post('id'));
        $status = intval($this->post('status'));

        try {
            $model = Model::findOne(['id' => $id]);
            $model->status = $status;
            $model->save();

            return $this->success();
        } catch (\Exception $e) {
            return $this->fail('操作错误: ' . $e->getMessage());
        }
    }

    public function actionChangeSort()
    {
        $id = intval($this->post('id'));
        $sort = intval($this->post('sort'));

        try {
            $model = Model::findOne(['id' => $id]);
            $model->sort = $sort;
            $model->save();

            return $this->success();
        } catch (\Exception $e) {
            return $this->fail('操作错误: ' . $e->getMessage());
        }
    }

    // public function actionBatchRemove()
    // {
    //     $ids = $this->post('ids');
    //
    //     try {
    //         Model::updateAll(['status' => Model::$statusMapping['DELETE']], ['id' => $ids]);
    //
    //         return $this->success();
    //     } catch (\Exception $e) {
    //         return $this->fail('删除错误: ' . $e->getMessage());
    //     }
    // }
}